package com.hmz.backend.service.Impl.chat;


import cn.hutool.json.JSONObject;
import com.hmz.backend.mapper.MessageMapper;
import com.hmz.backend.service.chat.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MessageServiceImpl implements MessageService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Autowired
    private MessageMapper messageMapper;
    @Value("${PageSize}")
    int pageSize;


    @Override
    public JSONObject getPrivateMessage(int chatRoomId) {
        ListOperations listOperations = redisTemplate.opsForList();
        List<String> list = null;
        try {
            list = listOperations.range("message"+chatRoomId,0,-1);
        } catch (Exception e) {
            System.out.println(e);
            return new JSONObject().set("message","没有消息！");
        }
        return new JSONObject().set("message","查询成功！").set("total_pages",getMaxPages(chatRoomId,pageSize)).set("list",list);
    }

    @Override
    public int  getMaxPages(int chatRoomId,int pageSize) {
        return messageMapper.getMaxPages("message"+chatRoomId,pageSize);
    }
    /**
     * 从MYsql里面拿数据
     * @param chatRoomId
     * @param pageNow
     * @param pageSize
     * @return
     */
    @Override
    public JSONObject getOldMessage(int chatRoomId, int pageNow, int pageSize) {
        return new JSONObject().set("message","成功").set("total_pages",getMaxPages(chatRoomId, pageSize)).set("list",messageMapper.getOldMessage("message"+chatRoomId,pageNow,pageSize));
    }


    /**
     * 获得指定聊天室下指定人的发言
     * @param chatRoomId
     * @param userId
     * @return
     */
    @Override
    public JSONObject getUserMessageListInChatRoom(int chatRoomId, int userId) {
//        QueryWrapper<Message> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("receiver_id",chatRoomId).eq("sender_id",userId);
        //List<Message> messageList = messageMapper.selectList(queryWrapper);
//        List<JSONObject> list = new ArrayList<>();
//        for (Message m:messageList) {
//            list.add(new JSONObject().set("userName",))
//        }
        List<JSONObject> list = messageMapper.selectShowList(chatRoomId, userId);
        return new JSONObject().set("message","查询成功").set("list",list);
    }

    /**
     * 获取指定聊天室下的日期列表
     * @param chatRoomId
     * @return
     */
    @Override
    public JSONObject getTimeListInChatRoom(int chatRoomId) {
//        QueryWrapper<Message> queryWrapper  = new QueryWrapper<>();
//        queryWrapper.select("create_time").eq("receiver_id",chatRoomId).groupBy("create_time").orderByDesc("create_time");
        List<JSONObject> list = messageMapper.getTimeListInChatRoom(chatRoomId);
        return new JSONObject().set("message","查询成功").set("list",list );
    }

    /**
     * 获取指定聊天室指定类型的消息列表
     * @param chatRoomId
     * @param type
     * @return
     */

    @Override
    public JSONObject getTypeMessageListInChatRoom(int chatRoomId, int type) {
//        QueryWrapper<Message> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("receiver_id",chatRoomId).eq("type",type);
        List<JSONObject> list = messageMapper.getTypeMessageListInChatRoom(chatRoomId, type);
        return new JSONObject().set("message","查询成功").set("list",list);
    }

    /**
     * 获取指定聊天室指定日期的消息列表
     * @param chatRoomId
     * @param date
     * @return
     */
    @Override
    public JSONObject getTimeMessageListInChatRoom(int chatRoomId, String date) {
        List<JSONObject> list = messageMapper.getTimeMessageListInChatRoom(chatRoomId, date);
        return new JSONObject().set("message","查询成功").set("list",list);
    }


}
